//
//  SLCAPropertyAnimationController.m
//  Objective-C
//
//  Created by 陆承东 on 2018/7/1.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLCAPropertyAnimationController.h"

@interface SLCAPropertyAnimationController ()<CAAnimationDelegate>

@property (nonatomic, weak) UIView *layerView;
@property (nonatomic, weak) CALayer *colorLayer;
@property (nonatomic, weak) UIButton *changeButton;

@end

@implementation SLCAPropertyAnimationController

- (void)initNavBar {
    self.navTitle = @"PropertyAnimation";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavBar];
    
    [self initView];
    
    [self animate5];
}

- (void)initView {
    UIView *layerView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    layerView.backgroundColor = [UIColor whiteColor];
    self.layerView = layerView;
    [self.view addSubview:layerView];
    
    CALayer *colorLayer = [CALayer layer];
    colorLayer.frame = CGRectMake(50, 50, 100, 100);
    colorLayer.backgroundColor = [UIColor blueColor].CGColor;
    self.colorLayer = colorLayer;
    // add it to our view
    [self.layerView.layer addSublayer:colorLayer];
    
    UIButton *changeButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 160, 100, 30)];
    [changeButton setTitle:@"Chage Color" forState:UIControlStateNormal];
    [changeButton setTitleColor:[UIColor colorWithHexString:@"#66ff66"] forState:UIControlStateNormal];
    changeButton.layer.borderWidth = 1;
    changeButton.layer.borderColor = [UIColor darkGrayColor].CGColor;
    self.changeButton = changeButton;
    [layerView addSubview:changeButton];
    
    [self animate1];
}

#pragma mark ----- 属性动画
- (void)animate1 {
    // create sublayer
    [self.changeButton addTarget:self action:@selector(changeButtonClick1:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)changeButtonClick1:(UIButton *)sender {
    // create a new random color
    CGFloat red = arc4random() / (CGFloat)INT_MAX;
    CGFloat green = arc4random() / (CGFloat)INT_MAX;
    CGFloat blue = arc4random() / (CGFloat)INT_MAX;
    UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
    
    // create a basic animation
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"backgroundColor";
    animation.toValue = (__bridge id)color.CGColor;
    animation.delegate = self;
    // apply animation to layer
    [self.colorLayer addAnimation:animation forKey:nil];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    if ([anim isKindOfClass:[CABasicAnimation class]]) {
        // set the backgroundColor property to match animation toValue
        CABasicAnimation *animation = (CABasicAnimation *)anim;
        [CATransaction begin];
        [CATransaction setDisableActions:true];
        self.colorLayer.backgroundColor = (__bridge CGColorRef)animation.toValue;
        [CATransaction commit];
    }
}

#pragma mark ----- 关键帧动画
- (void)animate2 {
    [self.changeButton addTarget:self action:@selector(changeButtonClick2:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)changeButtonClick2:(UIButton *)sender {
    // create a keyframe animation
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"backgroundColor";
    animation.duration = 2.0;
    animation.values = @[
                         (__bridge id)[UIColor blueColor].CGColor,
                         (__bridge id)[UIColor redColor].CGColor,
                         (__bridge id)[UIColor greenColor].CGColor,
                         (__bridge id)[UIColor blueColor].CGColor
                         ];
    // apply animation to layer;
    [self.colorLayer addAnimation:animation forKey:nil];
}

- (void)animate3 {
    UIBezierPath *bezierPath = [[UIBezierPath alloc] init];
    [bezierPath moveToPoint:CGPointMake(0, 150)];
    [bezierPath addCurveToPoint:CGPointMake(300, 150) controlPoint1:CGPointMake(75, 0) controlPoint2:CGPointMake(225, 300)];
    // draw the path using a CAShapeLayer
    CAShapeLayer *pathLayer = [CAShapeLayer layer];
    pathLayer.path = bezierPath.CGPath;
    pathLayer.fillColor = [UIColor clearColor].CGColor;
    pathLayer.strokeColor = [UIColor redColor].CGColor;
    pathLayer.lineWidth = 3.0;
    [self.layerView.layer addSublayer:pathLayer];
    
    // add the ship
    CALayer *shipLayer = [CALayer layer];
    shipLayer.frame = CGRectMake(0, 0, 32, 32);
    shipLayer.position = CGPointMake(0, 150);
    shipLayer.contents = (__bridge id)[UIImage imageNamed:@"nav_share"].CGImage;
    [self.layerView.layer addSublayer:shipLayer];
    
    // create the keyframe animation
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"position";
    animation.duration = 4.0;
    animation.path = bezierPath.CGPath;
    animation.rotationMode = kCAAnimationRotateAuto;
    [shipLayer addAnimation:animation forKey:nil];
}

- (void)animate4 {
    // add the ship
    CALayer *shipLayer = [CALayer layer];
    shipLayer.frame = CGRectMake(0, 0, 32, 32);
    shipLayer.position = CGPointMake(0, 150);
    shipLayer.contents = (__bridge id)[UIImage imageNamed:@"nav_share"].CGImage;
    [self.layerView.layer addSublayer:shipLayer];
    
    // animate the ship rotation
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"transform";
    animation.duration = 2.0;
    animation.byValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0, 0, 1)];
    [shipLayer addAnimation:animation forKey:nil];
}

- (void)animate5 {
    // add the ship
    CALayer *shipLayer = [CALayer layer];
    shipLayer.frame = CGRectMake(0, 0, 32, 32);
    shipLayer.position = CGPointMake(0, 150);
    shipLayer.contents = (__bridge id)[UIImage imageNamed:@"nav_share"].CGImage;
    [self.layerView.layer addSublayer:shipLayer];
    
    // animate the ship rotation
    CABasicAnimation *animation = [CABasicAnimation animation];
    animation.keyPath = @"transform.rotation";
    animation.duration = 2.0;
    animation.byValue = @(M_PI * 2);
    [shipLayer addAnimation:animation forKey:nil];
}

@end
